package com.laoniu.service.base;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.laoniu.dto.request.BaseCustomerRequestDto;
import com.laoniu.entity.BaseCustomer;
import com.laoniu.entity.SecurityUser;
import com.laoniu.mapper.BaseCustomerMapper;
import com.laoniu.utils.SpringSecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 客户信息表 服务实现类
 * </p>
 *
 * @author lxl
 * @since 2023-12-17
 */
@Service
public class BaseCustomerService extends ServiceImpl<BaseCustomerMapper, BaseCustomer> {

    @Resource
    private BaseCustomerMapper customerMapper;
    public Page<BaseCustomer> listPage(BaseCustomerRequestDto requestDto) {
        SecurityUser user = SpringSecurityUtils.getUser();
        Page<BaseCustomer> page = new Page<>(requestDto.getPageNo(), requestDto.getPageSize());
        LambdaQueryWrapper<BaseCustomer> wrapper = Wrappers.lambdaQuery(BaseCustomer.class);
        wrapper.like(StringUtils.isNotBlank(requestDto.getName()), BaseCustomer::getName, requestDto.getName());
        if (!user.isAdmin()){
            wrapper.eq(BaseCustomer::getOpId, user.getId());
        }
        wrapper.orderByDesc(BaseCustomer::getId);
        return customerMapper.selectPage(page, wrapper);
    }
}
